package com.xxl.job.client;

import com.xxl.job.core.biz.model.ParamInfo;
import com.xxl.job.core.biz.model.PropUtils;
import com.xxl.job.core.biz.model.TaskInfo;
import com.xxl.job.exception.JobException;
import org.springframework.util.StringUtils;

/**
 * TODO
 *
 * @author huangtao
 * @date 2020/7/4 11:36
 */
public abstract class AbstractTaskClient {

    /**
     * 请求超时 （秒）
     */
    protected final static int TIMEOUT = 3;

    /**
     * 创建 url
     */
    protected final static String URL_CREATE = "api/add";

    /**
     * 创建多任务 url
     */
    protected final static String URL_CREATE_BATCH = "api/add/batch";

    /**
     * 启动 url
     */
    protected final static String URL_START = "api/start";
    /**
     * 启动 url
     */
    protected final static String URL_START_LIST = "api/start/list";

    /**
     * 创建并启动 url
     */
    protected final static String URL_CREATE_START = "api/create/start";
    /**
     * 创建并启动多任务 url
     */
    protected final static String URL_CREATE_START_BATCH = "api/create/start/batch";

    /**
     * 停止 url
     */
    protected final static String URL_STOP = "api/stop";
    /**
     * 停止并删除任务 url
     */
    protected final static String URL_DELETE = "api/remove";
    /**
     * 更新任务 url
     */
    protected final static String URL_UPDATE = "api/update/list";

    public static String getUri(String path) {
        String url = PropUtils.getAdminAddresses();

        if (!url.endsWith("/")) {
            url = url + "/";
        }
        return url + path;
    }

    public static String getToken() {

        return PropUtils.getAccessToken();
    }

    protected void validate(TaskInfo info) {

        if (StringUtils.isEmpty(info.getGroupId()) || StringUtils.isEmpty(info.getHandler())) {
            throw new JobException("groupId or hanler is null");
        }
    }

    protected void validate(ParamInfo param) {
        if (StringUtils.isEmpty(param.getTaskGroupId()) || StringUtils.isEmpty(param.getExecutorHandler())) {
            throw new JobException("taskGroupId or executorHandler is null");
        }
    }
}
